<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *
 * @abstract 物料管理
 * @author zxj
 *
 */
//include_once  APPPATH.'/libraries/alibaba/request/queryWarrantiesByOrderId.php';
class Ajax extends CI_Controller
{
    private $t_material = 'material_list';  //物料表

    function __construct()
    {
        parent::__construct();
        ob_end_clean();
        $this->load->model('procurement_model');
        $this->load->model('ae_model');
        $this->load->model('Brand_model');
        $this->load->model('order_model','orderModel');
        $this->load->model('product_model');
        $this->load->model('users_model');
        $this->load->model('batchorder_model');
        $this->load->model('package_model');
        $this->load->model('product_sap_model','sapModel');
        $this->load->model('api_model/tom_account_model','account_model');
    }
    /**
     * 配件功能
     */
    function ajaxMaterial(){
        $searchParam = gp();
        $data = array();
        if($searchParam && $searchParam['keyword']){
            $mtId=substr($searchParam['mtid'],0,2);
            $url = 'http://stock.dhhtgroup.com/webService/GetComponentsByPar.php';
            $dataSap = array('account'=>'Tomrepair666', 'token'=>'TomrepairAccessory','data'=>$searchParam['keyword']);
            $materialList=CurlPost($url,$dataSap);
            $data['partList'] = $materialList['message'];
            $data['mtId']=$mtId;
            $data['productId']=$searchParam['productId'];
        }
        if(!is_array($materialList['message'])){
            exit('0');
        }
        $this->load->view('defaults/material_list',$data);
    }


    /**
     * 获取单个配件
     */
    function ajaxMaterialOne(){
        $gp = gp();
        //加载查询类
        $this->load->library('OmsQuery');
        $omsQuery = new OmsQuery();
        //获取虚拟仓代码
        $code=$omsQuery->GetProductWarehouseCode($gp['productId']);
        $url = 'http://stock.dhhtgroup.com/webService/GetComponentsByPar.php';
        $dataSap = array('account'=>'Tomrepair666', 'token'=>'TomrepairAccessory','sap_sku'=>$gp['sap_sku'], 'stockName'=>$code);
        $kc=CurlPost($url,$dataSap);
        echo $kc['message'];
    }
    /**
     * 修改采购单数据
     */
    public function updatecg(){
        $gp=gp();
        $arr=array(
            'name_cn'=>$gp['pjname'],
            'number'=>$gp['numeber'],
        );
        $shit= $this->procurement_model->updatecg($gp['id'],$arr);
        if($shit==1){
            echo $shit;
        }else{
            echo 0;
        }
        exit;
    }

    /**
     * 修改客户资料
     */
    public function updatekh($id){
        $gp=gp();
        $rt=$this->users_model->updatecustomerslist($id,$gp);
        if($rt){
            echo "1";
        }else{
            echo "2";
        }
    }
    /**
     * 获取品牌型号SKU
     */
    function getsku(){
        $gp=gp();
        $url = 'http://stock.dhhtgroup.com/webService/GetSkuByProductType.php';
        $dataArr = array('product'=>$gp['band'], 'type'=>$gp['model']);
        $data = array('account'=>'Tomrepair', 'token'=>'TomrepairSku','data'=>json_encode($dataArr));
        $skulist=$this->CurlPost($url,$data);
        echo json_encode($skulist['data']);
    }

    /**
     * 获取订单产品信息
     */
    public function GetAliexpressOrder(){
        $gp=gp();
        $product=array();
        $dataList=$this->ae_model->GetAliExpressWarrantyOrderList($gp['orderid']);
        $product['orderId']=$dataList['orderId'];
        $product['skuInfo']=$dataList['skuInfo'];
        $product['description']=$dataList['description'];
        $pro=json_decode($dataList['productProperties']);
        $proArray=(get_object_vars($pro));
        $product['BrandName']=$proArray['Brand Name'];
        $product['Model']=$proArray['Model']?$proArray['Model']:trim(str_replace($product['BrandName'],'',$proArray[$product['BrandName'].' Model']));
        $product['ROM']=$proArray['ROM'];
        $product['Display']=$proArray['Display Resolution'];
        $product['Item']=$proArray['Item Condition'];
        $product['Unlock']=$proArray['Unlock Phones'];
        $product['CPU']=$proArray['CPU'];
        $product['Battery']=$proArray['Battery Capacity(mAh)'];
        $product['DisplaySize']=$proArray['Display Size'];
        $product['RAM']=$proArray['RAM'];
        $product['attachments']=$dataList['attachments'];
        echo json_encode($product);
    }

    /**
     * 修改联系状态
     * @param $orderid
     */
    public function UpdateAeStatus(){
        $gp=gp();
        $this->ae_model->UpdateAeStatus($gp['orderid'],array('ctstatus'=>$gp['ctstatus'],'contact'=>$gp['contact'],'ctime'=>time()));
        $this->ae_model->UpdateAeFollowNum($gp['orderid']);
        switch($gp['ctstatus']){
            case '0':
                $color='';
                break;
            case '1':
                $color='#ff9a00';
                break;
            case '2':
                $color='#009900';
                break;
            case '3':
                $color='#0000ff';
                break;
            case '4':
                $color='#ff0000';
                break;
            case '5':
                $color='#ff33ff';
                break;
            default:
                $color='';
        }
        $data['color']=$color;
        $data['contact']=$gp['contact'];
        $data['time']=date('Y-m-d H:i:s',time());
        echo json_encode($data);
    }

    /**
     * 打印条码
     * @param string $oid
     */
    function printBarcode($oid = ''){
        $barcodeList = $this->common_model->selectCustom('print_barcode_list',array('oid'=>$oid));
        $data['barcodeList'] = $barcodeList;
        $data['oid']=$oid;
        $this->load->view('defaults/barcode_view', $data);
    }

    /**
     * CURL POST
     * @param $url
     * @param $data
     * @return mixed
     */
    public function CurlPost($url,$data){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER,true);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
        curl_setopt($ch, CURLOPT_URL, $url);
        $response=curl_exec($ch);
        curl_close($ch);
        $result = json_decode($response,true);
        return $result;
    }

    /**
     * 联系人列表
     */
    public function ContactList(){
        $gp=gp();
        $ContactList=$this->users_model->ContactList($gp['customers_id']);
        echo json_encode($ContactList);
    }
    /**
     * 生成折线图表数据
     */
    public function LineChartUpdate(){
        $timelinejson=array();//记录时间轴
        $timelinebl=array();//补零时间轴m
        $iconJson=array();//折线数据
        //记录时间轴
        $aliExpressTimeline=$this->ae_model->AliExpressTimeline();
        foreach($aliExpressTimeline as $row){
            $timelinejson[]=$row['times'];
            $timelinebl[]=strtotime($row['times']);
        }
        //各国家的日销售量
        $iconJson['es']=$this->linedata($timelinebl,'es');
        $iconJson['ru']=$this->linedata($timelinebl,'ru');
        $iconJson['de']=$this->linedata($timelinebl,'de');
        $iconJson['us']=$this->linedata($timelinebl,'us');
        $iconJson['uk']=$this->linedata($timelinebl,'uk');
        $iconJson['times']=$timelinejson;
        echo json_encode($iconJson);
        // file_put_contents('./resource/uploadfiles/file/json/aliexpress.json',json_encode($iconJson));
    }
    /**
     * 获取国家日销售量
     * @param $timeline
     * @param $state
     * @return array
     */
    public function linedata($timeline,$state){
        $aliExpressSalesEs=$this->ae_model->CountAliExpressSales($state);
        $iconJson=array();
        $iconBl=array();
        if($aliExpressSalesEs) {
            foreach ($aliExpressSalesEs as $row) {
                $iconBl[$state][strtotime($row['times'])] = $row['Sales'];
            }
            //数据补零
            foreach (array_diff($timeline, array_keys($iconBl[$state])) as $key => $val) {
                $iconBl[$state][$val] = 0;
            }
            ksort($iconBl[$state]);
            foreach($iconBl[$state] as $key=>$val){
                $iconJson[]=$val;
            }
        }else{
            //数据补零
            foreach ($timeline as $key => $val) {
                $iconJson[] = 0;
            }
        }
        return $iconJson;
    }

    /**
     * 获取速卖通型号
     */
    public function GetAliExpressModel(){
        $gp=gp();
        $data=$this->ae_model->GetAliExpressModel($gp['brand']);
        $modelStr="<span style='color:#0000ff'>已存在型号</span>：";
        $modelArray=array();
        foreach($data as $row){
            $modelStr .="<span style='color: #ff0000'>".$row['model']."</span>"."<span style='color: #0000ff'>----</span>";
            $modelArray[]=$row['model'];
        }
        $result['modelStr']=$modelStr;
        $result['modelArray']=$modelArray;
        echo json_encode($result);
    }

    /**
     * 获取订单号
     * @return string
     */
    public function GetOrderId(){
        $orderId=build_order_no();
        echo $orderId;
    }

    /**
     * 显示订单详情
     */
    public function GetBatchOrderInfo(){
        $gp=gp();
        $OrderInfo=$this->batchorder_model->GetBatchOrderInfo($gp['orderId']);
        echo json_encode($OrderInfo);
    }

    /**
     * 生成条码
     */
    public function CreateBarCode(){
        $gp=gp();
        $this->load->library('Barcode');
        $barcode=new Barcode();
        $barcodeHTML=$barcode->getBarcodeHTML(str_pad($gp['pid'], 8, "0", STR_PAD_LEFT),'C39');
        echo "<div style='width: 160px;height: 40px;letter-spacing:12px;'>".$barcodeHTML."<div style='margin-top: -2px;height:1px;'>".str_pad($gp['pid'], 8, "0", STR_PAD_LEFT)."</div>"."</div>";
    }

    /**
     * 包裹信息
     */
    public function PackageInfo(){
        $gp=gp();
        //包裹数据
        $packageData=$this->package_model->GetPackageInfo($gp['id']);
        if($packageData['time']) {
            $packageData['time'] = date('Y-m-d H:i:s', $packageData['time']);
        }else{
            $packageData['time']='';
        }
        //订单号
        $orderId=substr($packageData['order_id'],0,2);
        //产品数据
        $pid=explode(',',$packageData['product_id']);
        if($orderId == 'wx') {
            $productData = $this->package_model->GetPackageProductData_NEW($packageData['product_id']);
        }else{
            $productData = $this->package_model->GetPackageProduct($pid);
        }
        $data['packageData']=$packageData;
        $data['productData']=json_encode($productData);
        echo json_encode($data);
    }

    /**
     * 拆包裹
     */
    public function UnPack(){
        $gp=gp();
        //包裹数据
        $packageData=$this->package_model->GetPackageInfo($gp['package_id']);
        $id=$packageData['id'];
        unset($packageData['id']);
        //获取对应订单包裹总数
        $num=$this->package_model->CountPackageOrderNum($packageData['order_id']);
        $package_id=explode('_',$packageData['package_id']);
        $packageData['package_id']=$package_id[0].'_'.GetPackageNo($num);
        $packageData['weight']=0;
        $packageData['freight']=0;
        //处理产品id
        $oldPid=explode(',',$packageData['product_id']);
        $newPid=explode(',',$gp['pid']);
        $pid=array_diff($oldPid,$newPid);
        if($pid){
            $upPid=implode(',',$pid);
        }else{
            $upPid='';
        }
        $packageData['product_id']=$gp['pid'];
        $packageData['trace_id']='';
        $packageData['is_package']=0;
        $packageData['documents']='';
        $add=$this->package_model->AddPackageData($packageData);
        if($add){
            $this->package_model->UpdatePackageData($id,array('product_id'=>$upPid));
            echo 1;
        }else{
            echo 2;
        }
    }

    /**
     * 获取SAP产品
     */
    public function GetSapProductList(){
        $gp=gp();
        $typeId=array();
        $customId=array();
        //获取SAP产品类型
        $sapData=$this->sapModel->SapProductList();
        //获取自定义产品类型
        $customData=$this->sapModel->GetSiteCustomTypeList();
        if($customData) {
            foreach ($customData as $val) {
                $sapData[] = array(
                    'name_en' => $val['name_en'],
                    'name' => $val['name_ch'],
                    'sap_id' => $val['custom_id']
                );
            }
        }
        //获取站点产品类型
        $siteData=$this->sapModel->GetSiteProductType($gp['site']);
        //保存已分配的产品类型
        foreach($siteData as $row){
            if($row['custom_id'] != '0') {
                $customId[] = $row['custom_id'];
            }else{
                $typeId[]=$row['tid'];
            }
        }
        //重构数组，对已分配类型进行disabled，避免重复添加(需前后端双向验证)
        foreach($sapData as &$row2){
            if(in_array($row2['sap_id'],$typeId) || in_array($row2['sap_id'],$customId)){
                $row2['ck']='disabled';
                $row2['y_n']="<span style='color: #00CC00'> √</span>";
            }else{
                $row2['ck']='';
                $row2['y_n']='';
            }
        }
        //释放多余物理内存
        unset($row2);
        //将结果集转JSON至前端
        echo json_encode($sapData);
    }

    /**
     * 获取站点产品类型信息
     */
    public function GetSiteTypeInfo(){
        $gp=gp();
        $TypeInfo['productType']=$this->sapModel->GetSiteTypeInfo($gp['id']);
        $TypeInfo['productRemark']=$this->sapModel->GetSiteProductRemark($gp['id'],'type_id');
        echo json_encode($TypeInfo);
    }
    /**
     * 获取SAP品牌
     */
    public function GetSapProductBrand(){
        $gp=gp();
        $brandId=array();
        $sapData=$this->sapModel->SapBrandList($gp['tid']);
        //新增OTHER品牌
        $sapData[count($sapData)]=array(
            'number'=>'01',
            'name_en'=>'OTHER',
            'name'=>'OTHER',
            'sap_id'=>1
        );
        $siteData=$this->sapModel->GetSiteProductBrand($gp['site'],$gp['tid'],$gp['custom_id']);
        //保存已分配的产品类型
        foreach($siteData as $row){
            $brandId[]=$row['bid'];
        }
        //重构数组，对已分配类型进行disabled，避免重复添加(需前后端双向验证)
        foreach($sapData as &$row2){
            if(in_array($row2['sap_id'],$brandId)){
                $row2['ck']='disabled';
                $row2['y_n']="<span style='color: #00CC00'> √</span>";
            }else{
                $row2['ck']='';
                $row2['y_n']="";
            }
        }
        //释放多余物理内存
        unset($row2);
        //将结果集转JSON至前端
        echo json_encode($sapData);
    }

    /**
     * 获取站点品牌信息
     */
    public function GetSiteBrandInfo(){
        $gp=gp();
        $brandInfo['productInfo']=$this->sapModel->GetSiteBrandInfo($gp['id']);
        $brandInfo['productRemark']=$this->sapModel->GetSiteProductRemark($gp['id'],'brand_id');
        echo json_encode($brandInfo);
    }

    /**
     * 新增站点品牌系列
     */
    public function AddSiteProductSeries(){
        $gp=gp();
        $this->sapModel->AddSiteProductSeries($gp);
        $data=$this->sapModel->GetSiteSeriesList($gp['site'],$gp['bid']);
        echo json_encode($data);
    }

    /**
     * 获取系列
     */
    public function GetSiteSeriesList(){
        $gp=gp();
        $data=$this->sapModel->GetSiteSeriesList($gp['site'],$gp['bid']);
        echo json_encode($data);
    }

    /**
     * 获取SAP型号
     */
    public function GetSapProductModel(){
        $gp=gp();
        $modelId=array();
        $sapData=$this->sapModel->SapModelList($gp['bid'],$gp['tid']);
        $siteData=$this->sapModel->GetSiteProductModel($gp['site'],$gp['bid'],$gp['tid'],$gp['custom_id']);
        //保存已分配的产品类型
        foreach($siteData as $row){
            $modelId[]=$row['mid'];
        }
        //重构数组，对已分配类型进行disabled，避免重复添加(需前后端双向验证)
        foreach($sapData as &$row2){
            if(in_array($row2['sap_id'],$modelId)){
                $row2['ck']='disabled';
                $row2['y_n']="<span style='color: #00CC00'> √</span>";
            }else{
                $row2['ck']='';
                $row2['y_n']="";
            }
        }
        //释放多余物理内存
        unset($row2);
        //将结果集转JSON至前端
        echo json_encode($sapData);
    }

    /**
     * 获取站点型号信息
     */
    public function GetSiteModelInfo(){
        $gp=gp();
        $modelInfo['productInfo']=$this->sapModel->GetSiteModelInfo($gp['id']);
        $modelInfo['productRemark']=$this->sapModel->GetSiteProductRemark($gp['id'],'model_id');
        echo json_encode($modelInfo);
    }

    /**
     * 获取站点属性
     */
    public function GetSiteAttribute(){
        $gp=gp();
        $siteData=$this->sapModel->GetSiteAttribute($gp['site'],$gp['tid'],$gp['bid'],$gp['mid']);
        echo json_encode($siteData);
    }

    /**
     * 添加站点属性
     */
    public function AddSiteAttribute(){
        $gp=gp();
        $result=$this->sapModel->AddSiteAttribute($gp);
        if($result){
            $data=$this->sapModel->GetSiteAttribute($gp['site'],$gp['tid'],$gp['bid'],$gp['mid']);
            echo json_encode($data);
        }
    }

    /**
     * 删除属性
     */
    public function DeleteAttribute(){
        $gp=gp();
        $this->sapModel->DeleteSiteAttribute($gp['id']);
        $data=$this->sapModel->GetSiteAttribute($gp['site'],$gp['tid'],$gp['bid'],$gp['mid']);
        echo json_encode($data);
    }

    /**
     * 获取站点自定义类型
     */
    public function GetSiteCustomTypeList(){
        $data=$this->sapModel->GetSiteCustomTypeList();
        echo json_encode($data);
    }

    /**
     * tomRepair账号信息
     */
    public function TrAccountInfo(){
        $gp=gp();
        //获取用户详情
        $accountInfo=$this->account_model->AccountInfo($gp['id']);
        //判断账号类型
        if($gp['type_id']){
            $type_id=$gp['type_id'];
        }else{
            $type_id=$accountInfo['role']?$accountInfo['role']:1;
        }
        //获取店铺数据
        $shopData=$this->account_model->GetShopList($type_id);
        //账号店铺集
        $shopIds=array();
        if($accountInfo['branch']){
            $shopIds=explode(',',$accountInfo['branch']);
        }
        //循环判断已选店铺
        foreach($shopData as &$row){
            if(in_array($row['id'],$shopIds)){
                $row['ck']='checked';
                $row['y_n']="<span style='color: #00CC00'> √</span>";
            }else{
                $row['ck']='';
                $row['y_n']="";
            }
        }
        unset($row);
        $data['accountInfo']=$accountInfo;
        $data['shopData']=$shopData;
        echo json_encode($data);
    }

    /**
     *推送Type数据至tomRepair
     */
    public function PostTypeTomRepair(){
        $gp=gp();
        $data=$this->sapModel->SelectBatchData($gp['ids'],'type');
        if(ENVIRONMENT=='development'){
            $url="http://test.dhhtgroup.com:8099/api/index.php/updateProductType";
        }else{
            $url = "http://www.tomrepair.es/api/index.php/updateProductType";
        }
        if($data) {
            foreach($data as $row) {
                $data_to_post = array();
                $data_to_post['user'] = 'api@tomrepair.es';
                $data_to_post['token'] = '78de933bd0fbacf02ef2de1116e3338f1c627b07';
                $product_data['product_type_id'] = $row['tid'];
                $product_data['product_type_status'] = $row['authorization'];
                $product_data['product_type_name'] = $row['name'];
                $product_data['product_type_name_en'] = $row['name_en'];
                $product_data['product_type_site'] = $row['site'];
                $product_data['product_type_sort_id'] = $row['sort_id'];
                $product_data['product_type_logo'] = $row['logo'];
                $product_data['is_custom'] = $row['is_custom'];
                $product_data['oms_id'] = $row['custom_id'];
                $data_to_post['product'] = array('0' => $product_data);
                $result = TomRepairCurlPost($url, $data_to_post);
                if($result['error_code'] == '100'){
                    $this->sapModel->UpdateTypeInfo($row['id'],array('is_sync'=>1));
                }else{
                    echo '推送信息至TomRepair失败 <'.$result['error_msg'].'>_5';
                    exit();
                }
            }
            echo '推送信息至TomRepair成功<'.$result['error_msg'].'>_6';
        }
    }

    /**
     *推送Brand数据至tomRepair
     */
    public function PostBrandTomRepair(){
        $gp=gp();
        $data=$this->sapModel->SelectBatchData($gp['ids'],'brand');
        if(ENVIRONMENT=='development'){
            $url="http://test.dhhtgroup.com:8099/api/index.php/updateProductBrand";
        }else{
            $url = "http://www.tomrepair.es/api/index.php/updateProductBrand";
        }
        if($data) {
            foreach($data as $row) {
                $data_to_post = array();
                $data_to_post['user'] = 'api@tomrepair.es';
                $data_to_post['token'] = '78de933bd0fbacf02ef2de1116e3338f1c627b07';
                $product_data['product_type_id'] = $row['tid'];
                $product_data['product_brand_id'] = $row['bid'];
                $product_data['product_brand_status'] = $row['authorization'];
                $product_data['product_brand_name'] = $row['name'];
                $product_data['product_brand_name_en'] = $row['name'];
                $product_data['product_brand_site'] = $row['site'];
                $product_data['product_brand_sort_id'] = $row['sort_id'];
                $product_data['product_brand_logo'] = $row['logo'];
                $product_data['custom_id'] = $row['custom_id'];
                $data_to_post['product'] = array('0'=>$product_data);
                $result = TomRepairCurlPost($url, $data_to_post);
                if($result['error_code'] == '100'){
                    $this->sapModel->UpdateBrandInfo($row['id'],array('is_sync'=>1));
                }else{
                    echo '推送信息至TomRepair失败 <'.$result['error_msg'].'>_5';
                    exit();
                }
            }
            echo '推送信息至TomRepair成功<'.$result['error_msg'].'>_6';
        }
    }


    /**
     *推送Model数据至tomRepair
     */
    public function PostModelTomRepair(){
        $gp=gp();
        $data=$this->sapModel->SelectBatchData($gp['ids'],'model');
        $isN=1;
        if(ENVIRONMENT=='development'){
            $url="http://test.dhhtgroup.com:8099/api/index.php/updateProductModel";
            $attributeUrl="http://test.dhhtgroup.com:8099/api/index.php/updateAttribute";
        }else{
            $url = "http://www.tomrepair.es/api/index.php/updateProductModel";
            $attributeUrl="http://www.tomrepair.es/api/index.php/updateAttribute";
        }
        if($data) {
            foreach($data as $row) {
                $data_to_post = array();
                $data_to_post['user'] = 'api@tomrepair.es';
                $data_to_post['token'] = '78de933bd0fbacf02ef2de1116e3338f1c627b07';
                $product_data['product_type_id'] = $row['tid'];
                $product_data['product_model_id'] = $row['mid'];
                $product_data['product_brand_id'] = $row['bid'];
                $product_data['product_model_status'] = $row['authorization'];
                $product_data['product_model_name'] = $row['name'];
                $product_data['product_model_name_en'] = $row['name'];
                $product_data['product_model_site'] = $row['site'];
                $product_data['product_model_sort_id'] = $row['sort_id'];
                $product_data['product_model_logo'] = $row['tid'];
                $product_data['product_model_series_id'] = $row['series_id'];
                $product_data['product_model_series_name'] = $row['series_name'];
                $product_data['product_model_warranty'] = $row['warranty_type'];
                $product_data['custom_id'] = $row['custom_id'];
                $data_to_post['product'] = array('0'=>$product_data);
                $result = TomRepairCurlPost($url, $data_to_post);
                if($result['error_code'] == '100'){
                    //更新推送状态
                    $this->sapModel->UpdateModelInfo($row['id'],array('is_sync'=>1));
                    //获取属性列表
                    $attributeData=$this->sapModel->GetSiteAttribute($row['site'],$row['tid'],$row['bid'],$row['mid']);
                    //推送属性信息
                    if($attributeData) {
                        //生成属性推送结构
                        $data_to_post_B['user'] = 'api@tomrepair.es';
                        $data_to_post_B['token'] = '78de933bd0fbacf02ef2de1116e3338f1c627b07';
                        foreach ($attributeData as $rowB) {
                            $dataB['id'] = $rowB['id'];
                            $dataB['model_id'] = $rowB['mid'];
                            $dataB['type_id'] = $rowB['tid'];
                            $dataB['name'] = $rowB['name'];
                            $dataB['site'] = $rowB['site'];
                            $dataB['brand_id'] = $rowB['bid'];
                            $data_to_post_B['data'][] = $dataB;
                        }
                        $resultB = TomRepairCurlPost($attributeUrl, $data_to_post_B);
                        if ($resultB['error_code'] != '100') {
                            echo '推送信息至TomRepair失败 <' . $result['error_msg'] . '>_5';
                            exit();
                        }
                    }
                }else{
                    echo '推送信息至TomRepair失败 <'.$result['error_msg'].'>_5';
                    exit();
                }
            }
            echo '推送信息至TomRepair成功<'.$result['error_msg'].'>_6';
        }
    }

    /**
     * 获取OMS产品三大类数据
     */
    public function GetOMSClassData(){
        $gp=gp();
        $val=explode('_',$gp['tid']);
        $tid=$val[0];
        if($val[1] != 0) {
            $custom_id = $val[1] . '_' . $val[2];
        }else{
            $custom_id='0';
        }
        if($gp['bid']){
            $bid=explode('_',$gp['bid']);
            $data['modelData']=$this->sapModel->GetSiteProductModel(6,$bid[0],$tid,$custom_id,1,'name');
        }else{
            $data['brandData']=$this->sapModel->GetSiteProductBrand(6,$tid,$custom_id,1,'name');
        }
        echo json_encode($data);
    }

    /**
     * 获取产品SKU
     */
    public function GetSapSku(){
        $gp=gp();
        $tid=explode('_',$gp['tid']);
        $bid=explode('_',$gp['bid']);
        $mid=explode('_',$gp['mid']);
        $url = 'http://stock.dhhtgroup.com/webService/GetSapSkuByProductClass.php';
        $data = array('account'=>'OMS','token'=>'OMS1234', 'bc'=>$tid[0], 'mc'=>$bid[0], 'sc'=>$mid[0]);
        $skuData=CurlPost($url,$data);
        echo json_encode($skuData);
    }

    /**
     * 客户列表
     */
    public function ClientList(){
        $gp=gp();
        $clientData=$this->account_model->GetShopList($gp['clientType']);
        echo json_encode($clientData);
    }
    /**
     * 客户信息
     */
    public function clientInfo(){
        $gp=gp();
        $clientInfo=$this->users_model->GetClientInfo($gp['id']);
        $parentID=$this->common_model->selectFirst('mt_customers',array('parent_id'=>$clientInfo['id']));
        $ids=$parentID?array($clientInfo['id'],$parentID['id']):array($clientInfo['id']);
        $parentList=$this->users_model->parentList($ids);
        $data=array('clientInfo'=>$clientInfo,'parentList'=>$parentList);
        echo json_encode($data);
    }
    /**
     * 获取仓库信息
     * @param $brand_client_id
     */
    public function AddWarehouse(){
        $gp=gp();
        $data=$this->users_model->GetWarehouseList($gp['brand_client_id']);
        echo json_encode($data);
    }
    /**
     * 币种验证
     */
    public function currencyPd(){
        $gp=gp();
        $currency=$this->users_model->Getcurrency($gp['customers_id']);
        if($currency) {
            echo $currency;
        }else{
            echo 'null';
        }
    }

    /**
     * 获取OMS全部产品品牌型号（非站点）
     */
    public function getProductClassAll(){
        $gp=gp();
        $typeID=explode('_',$gp['tid']);
        if($gp['bid']) {
            //查询型号
            $modelList = $this->sapModel->SapModelList($gp['bid'], $typeID[1]);
            $data=$modelList;
        }else {
            //查询品牌
            $brandList = $this->sapModel->SapBrandList($typeID[1]);
            $data=$brandList;
        }
        if($data) {
            echo json_encode($data);
        }else{
            echo 101;
        }
    }

    /**
     *验证维修项中英文名称
     */
    public function verifyRepairItemName(){
        $gp=gp();
        $where_ch=array('category'=>$gp['tid'],'name_ch'=>$gp['name_ch']);
        $where_en=array('category'=>$gp['tid'],'name_en'=>$gp['name_en']);
        $data_ch=$this->sapModel->verifyRepairItemName($where_ch);
        if($data_ch){
            echo 1;
            return;
        }
        $data_en=$this->sapModel->verifyRepairItemName($where_en);
        if($data_en){
            echo 2;
            return;
        }
        echo 3;
    }

    /**
     * 环球易购信息同步
     */
    public function GlobalEGrowSync(){
        $gp=gp();
        $this->load->library('GlobalEGrow');
        $globalEGrow = new GlobalEGrow();
        $data=array();
        if($gp['RepairInfo']){
            $data['RepairInfo']=$globalEGrow->transferRepairInfo($gp['pid']);
        }
        if($gp['Address']){
            $data['Address']=$globalEGrow->transferAddress($gp['pid']);
        }
        if($gp['saveFittingInfo']){
            $data['saveFittingInfo']=$globalEGrow->transferAccessories($gp['pid']);
        }
        if($gp['PayInfo']){
            $data['PayInfo']=$globalEGrow->transferPayInfo($gp['pid'],$gp['status']);
        }
        if($gp['Void']){
            $data['Void']=$globalEGrow->transferVoid($gp['pid']);
        }
        echo $data?json_encode($data):101;
    }
}
